// pages/fenlei/tags/tags.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['综合排序', '销量优先', '评分优先', '折扣优先'],
    index: 0,
    g:['1','2','3'],
  },
  bindtest: function (e) {
    console.log(this.data)
    var shuzu = [this.data.discount1,this.data.discount2,this.data.discount3]
    var shuzu2 = [this.data.id1,this.data.id2,this.data.id3]
    console.log(shuzu)
    console.log(shuzu2)
    for(let i = 0;i<2;i++){
      for(let j = 0;j<2-i;j++){
        if(shuzu[j]>shuzu[j+1]){
          var zj = shuzu2[j]
          shuzu2[j] = shuzu2[j+1]
          shuzu2[j+1] = zj
          zj = shuzu[j]
          shuzu[j] = shuzu[j+1]
          shuzu[j+1] = zj
        }
      }
    }
    console.log(shuzu)
    console.log(shuzu2)
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
    })
    if(e.detail.value==0){
      console.log(this.data)
      var shuzu = [this.data.sales1,this.data.sales2,this.data.sales3]
      var shuzu2 = [this.data.id1,this.data.id2,this.data.id3]
      console.log(shuzu)
      console.log(shuzu2)
      for(let i = 0;i<2;i++){
        for(let j = 0;j<2-i;j++){
          if(shuzu2[j]>shuzu2[j+1]){
            var zj = shuzu2[j]
            shuzu2[j] = shuzu2[j+1]
            shuzu2[j+1] = zj
            zj = shuzu[j]
            shuzu[j] = shuzu[j+1]
            shuzu[j+1] = zj
          }
        }
      }
      console.log(shuzu)
      console.log(shuzu2)
      this.setData({
        index1: e.detail.value,
        id1 : shuzu2[0],
        id2 : shuzu2[1],
        id3 : shuzu2[2]
      })
    }
    if(e.detail.value==1){
      console.log(this.data)
      var shuzu = [this.data.sales1,this.data.sales2,this.data.sales3]
      var shuzu2 = [this.data.id1,this.data.id2,this.data.id3]
      console.log(shuzu)
      console.log(shuzu2)
      for(let i = 0;i<2;i++){
        for(let j = 0;j<2-i;j++){
          if(shuzu[j]<shuzu[j+1]){
            var zj = shuzu2[j]
            shuzu2[j] = shuzu2[j+1]
            shuzu2[j+1] = zj
            zj = shuzu[j]
            shuzu[j] = shuzu[j+1]
            shuzu[j+1] = zj
          }
        }
      }
      console.log(shuzu)
      console.log(shuzu2)
      this.setData({
        index1: e.detail.value,
        id1 : shuzu2[0],
        id2 : shuzu2[1],
        id3 : shuzu2[2]
      })
    }
    if(e.detail.value==2){
      console.log(this.data)
      var shuzu = [this.data.grade1,this.data.grade2,this.data.grade3]
      var shuzu2 = [this.data.id1,this.data.id2,this.data.id3]
      console.log(shuzu)
      console.log(shuzu2)
      for(let i = 0;i<2;i++){
        for(let j = 0;j<2-i;j++){
          if(shuzu[j]<shuzu[j+1]){
            var zj = shuzu2[j]
            shuzu2[j] = shuzu2[j+1]
            shuzu2[j+1] = zj
            zj = shuzu[j]
            shuzu[j] = shuzu[j+1]
            shuzu[j+1] = zj
          }
        }
      }
      console.log(shuzu)
      console.log(shuzu2)
      this.setData({
        index1: e.detail.value,
        id1 : shuzu2[0],
        id2 : shuzu2[1],
        id3 : shuzu2[2]
      })
    }
    if(e.detail.value==3){
      console.log(this.data)
      var shuzu = [this.data.discount1,this.data.discount2,this.data.discount3]
      var shuzu2 = [this.data.id1,this.data.id2,this.data.id3]
      console.log(shuzu)
      console.log(shuzu2)
      for(let i = 0;i<2;i++){
        for(let j = 0;j<2-i;j++){
          if(shuzu[j]>shuzu[j+1]){
            var zj = shuzu2[j]
            shuzu2[j] = shuzu2[j+1]
            shuzu2[j+1] = zj
            zj = shuzu[j]
            shuzu[j] = shuzu[j+1]
            shuzu[j+1] = zj
          }
        }
      }
      console.log(shuzu)
      console.log(shuzu2)
      this.setData({
        index1: e.detail.value,
        id1 : shuzu2[0],
        id2 : shuzu2[1],
        id3 : shuzu2[2]
      })
    }
    wx.navigateTo({
      url: '../tags2/tags2'
    })
  },
  click_back: function () {
    wx.navigateTo({
      url: '../fenlei'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let pages = getCurrentPages();
    let { data } = pages[pages.length - 2]
    console.log(data)
    let { id1 } = data
    // 调用云函数

    wx.cloud.callFunction({
      name: 'get',
      data: { id: id1 },
      success: res => {
        console.log(res.result)
        let { data } = res.result
        console.log(data)
        console.log('[云函数] [add] user', data[0])
        let { author, discount, grade, name, price, sales, url } = data[0]
        this.setData({
          author1: author,
          discount1: discount,
          grade1: grade,
          name1: name,
          price1: price,
          sales1: sales,
          url1: url,

        })
      },
      fail: err => {
        console.error('[云函数] [add] 调用失败', err)
      }
    })
    let { id2 } = data
    // 调用云函数
    wx.cloud.callFunction({
      name: 'get',
      data: { id: id2 },
      success: res => {
        console.log(res.result)
        let { data } = res.result
        console.log(data)
        console.log('[云函数] [add] user', data[0])
        let { author,discount, grade, name, price, sales, url } = data[0]
        this.setData({
          author2: author,
          discount2: discount,
          grade2: grade,
          name2: name,
          price2: price,
          sales2: sales,
          url2: url
        })
      },
      fail: err => {
        console.error('[云函数] [add] 调用失败', err)
      }
    })
    let { id3 } = data
    // 调用云函数
    wx.cloud.callFunction({
      name: 'get',
      data: { id: id3 },
      success: res => {
        console.log(res.result)
        let { data } = res.result
        console.log(data)
        console.log('[云函数] [add] user', data[0])
        let { author, discount, grade, name, price, sales, url } = data[0]
        this.setData({
          author3: author,
          discount3: discount,
          grade3: grade,
          name3: name,
          price3: price,
          sales3: sales,
          url3: url
        })
      },
      fail: err => {
        console.error('[云函数] [add] 调用失败', err)
      }
     
    })
   // console.log(this.data.grade1)
   this.setData({
    id1:id1,
    id2:id2,
    id3:id3
  })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})